package com.ttg.web.dao.Impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ttg.common.constant.BasicConstant;
import com.ttg.common.entities.BasePage;
import com.ttg.model.mapper.AdminLogMapper;
import com.ttg.model.pojo.AdminLog;
import com.ttg.web.dao.AdminLogDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author ZQ
 * @version 1.0
 * @Description
 * @date 2021/2/22 10:59
 */

@Service
public class AdminLogDaoImpl extends ServiceImpl<AdminLogMapper, AdminLog> implements AdminLogDao {

    @Override
    public boolean addLog(long adminId, Long chlId, String curd, Long objId, String remark, String ip) {
        return addLog(adminId,chlId,curd,objId,remark,ip,null,null,null);
    }

    @Override
    public boolean addLog(long adminId, Long chlId, String curd, Long objId, String remark,String ip,String admUserName ,String admTrueName,String chlName) {
        chlId = Optional.ofNullable(chlId).orElse(BasicConstant.LONG_0);
        objId = Optional.ofNullable(objId).orElse(BasicConstant.LONG_0);
        remark = Optional.ofNullable(remark).orElse("--");
        curd = Optional.ofNullable(curd).orElse("r");
        ip = Optional.ofNullable(ip).orElse("--");
        AdminLog al = new AdminLog();
        al.setLogAdmId(adminId);
        al.setLogChlId(chlId);
        al.setLogCurd(curd);
        al.setLogObjId(objId);
        al.setLogRemark(remark);
        al.setLogIp(ip);
        al.setLogTime(LocalDateTime.now());
        al.setAdmUserName(admUserName);
        al.setAdmTrueName(admTrueName);
        al.setChlName(chlName);
        return save(al);
    }


    @Override
    public BasePage<AdminLog> findLogPage(BasePage<AdminLog> basePage, Long admId, List<Long> adminAgentNo, Integer admType, Long admOrgNo,
                                          List<Long> chlIds, LocalDateTime sdate, LocalDateTime edate) {
        LambdaQueryWrapper<AdminLog> wrapper = new LambdaQueryWrapper();
        wrapper.select(AdminLog::getAdmUserName,AdminLog::getAdmTrueName,AdminLog::getChlName,AdminLog::getLogId,
                AdminLog::getLogObjId,AdminLog::getLogCurd,AdminLog::getLogRemark,AdminLog::getLogTime,AdminLog::getLogIp)
                .eq(Objects.nonNull(admId),AdminLog::getLogAdmId,admId)
                .in(!CollectionUtils.isEmpty(chlIds),AdminLog::getLogChlId,chlIds)
                .gt(Objects.nonNull(sdate),AdminLog::getLogTime,sdate)
                .gt(Objects.nonNull(edate),AdminLog::getLogTime,edate);
        return page(basePage,wrapper);
    }

    @Override
    public BasePage<AdminLog> findAdminLogPage(BasePage<AdminLog> parse, Long admId, String admUserName, String admTrueName,
                                                          Long logObjId, List<Long> logChlIds, LocalDate sDate, LocalDate eDate,List<Long> adminIds) {
        LambdaQueryWrapper<AdminLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(AdminLog::getAdmUserName,AdminLog::getAdmTrueName,AdminLog::getLogChlId,AdminLog::getLogId,AdminLog::getLogObjId,
                AdminLog::getLogCurd,AdminLog::getLogRemark,AdminLog::getLogTime,AdminLog::getLogIp)
                .eq(Objects.nonNull(admId),AdminLog::getLogAdmId,admId)
                .likeLeft(StringUtils.isNotBlank(admUserName),AdminLog::getAdmUserName,admUserName)
                .likeLeft(StringUtils.isNotBlank(admTrueName),AdminLog::getAdmTrueName,admTrueName)
                .eq(Objects.nonNull(logObjId),AdminLog::getLogObjId,logObjId)
                .in(!CollectionUtils.isEmpty(logChlIds),AdminLog::getLogChlId,logChlIds)
                .gt(Objects.nonNull(sDate),AdminLog::getLogTime,sDate)
                .lt(Objects.nonNull(eDate),AdminLog::getLogTime,eDate)
                .in(!CollectionUtils.isEmpty(adminIds),AdminLog::getLogAdmId,adminIds)
                .orderByDesc(AdminLog::getLogId);
        return page(parse,wrapper);
    }


}
